package com.inf.controller;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.inf.service.PersonService;
import com.inf.utils.HBaseUtil;
import com.inf.vo.BaseVo;
import com.inf.vo.PersonBaseVo;
import com.inf.vo.ResponseVo;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by zhangbn on 2018/11/14.
 */
@Controller
@ResponseBody
@RequestMapping("/personface/common")
public class PersonController {

    Logger log = LoggerFactory.getLogger(PersonController.class);

    @Autowired
    private PersonService personService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping(value = "/cache", method = RequestMethod.POST)
    public ResponseVo cache(@RequestParam("beginTime") String begin, @RequestParam("endTime") String end) throws IOException {
        log.info("\n\n");
        log.info("show: 开始从HBase人员库bigdata:person-repo中缓存[" + begin + " - " + end + "]之间的数据到Redis......");
        log.info("\n\n");
        Jedis jedis = jedisPool.getResource();
        List<PersonBaseVo> voList = new ArrayList<PersonBaseVo>();
        Table table = HBaseUtil.connection.getTable(TableName.valueOf("bigdata:person-repo"));
        Scan scan = new Scan();
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter format_use = DateTimeFormatter.ofPattern("yyMMddHHmmss");
        LocalDateTime startTime = LocalDateTime.parse(begin, format);
        LocalDateTime endTime = LocalDateTime.parse(end, format);
        String startStr = startTime.format(format_use);
        String endStr = endTime.format(format_use);
        scan.setStartRow(Bytes.toBytes(startStr));
        scan.setStopRow(Bytes.toBytes(endStr));
        ResultScanner scanner = table.getScanner(scan);
        int sum = 0;
        for (Result result : scanner) {
            String rowkey = Bytes.toString(result.getRow());
            String feature = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("feature")));
            jedis.rpush("rowkeyList", rowkey);
            jedis.rpush("featureList", feature);
            sum++;
        }
        IOUtils.closeQuietly(table);
        ResponseVo rst = new ResponseVo();
        Map<String, Integer> mp = new HashMap<>();
        mp.put("sum", sum);
        rst.setData(mp);
        log.info("\n\n");
        log.info("show: 从HBase人员库bigdata:person-repo中缓存[" + begin + " - " + end + "]之间，共sum= " + sum + " 条数据到Redis成功");
        log.info("\n\n");
        return rst;
    }


    @RequestMapping(value = "/query/{limit}")
    public List<BaseVo> query(@PathVariable("limit") String limit) {
        log.info(jedisPool.getResource().get("key1"));

        return personService.query(Integer.valueOf(limit));
    }


}